package com.sugar.subscribe.course.module.business.controller.admin.city;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.sugar.subscribe.course.framework.common.pojo.PageParam;
import com.sugar.subscribe.course.framework.common.pojo.PageResult;
import com.sugar.subscribe.course.framework.common.pojo.CommonResult;
import com.sugar.subscribe.course.framework.common.util.object.BeanUtils;
import static com.sugar.subscribe.course.framework.common.pojo.CommonResult.success;

import com.sugar.subscribe.course.framework.excel.core.util.ExcelUtils;

import com.sugar.subscribe.course.framework.apilog.core.annotation.ApiAccessLog;
import static com.sugar.subscribe.course.framework.apilog.core.enums.OperateTypeEnum.*;

import com.sugar.subscribe.course.module.business.controller.admin.city.vo.*;
import com.sugar.subscribe.course.module.business.dal.dataobject.city.CityDO;
import com.sugar.subscribe.course.module.business.service.city.CityService;

@Tag(name = "管理后台 - 城市")
@RestController
@RequestMapping("/business/city")
@Validated
public class CityController {

    @Resource
    private CityService cityService;

    @PostMapping("/create")
    @Operation(summary = "创建城市")
    @PreAuthorize("@ss.hasPermission('business:city:create')")
    public CommonResult<Long> createCity(@Valid @RequestBody CitySaveReqVO createReqVO) {
        return success(cityService.createCity(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新城市")
    @PreAuthorize("@ss.hasPermission('business:city:update')")
    public CommonResult<Boolean> updateCity(@Valid @RequestBody CitySaveReqVO updateReqVO) {
        cityService.updateCity(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除城市")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:city:delete')")
    public CommonResult<Boolean> deleteCity(@RequestParam("id") Long id) {
        cityService.deleteCity(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除城市")
                @PreAuthorize("@ss.hasPermission('business:city:delete')")
    public CommonResult<Boolean> deleteCityList(@RequestParam("ids") List<Long> ids) {
        cityService.deleteCityListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得城市")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:city:query')")
    public CommonResult<CityRespVO> getCity(@RequestParam("id") Long id) {
        CityDO city = cityService.getCity(id);
        return success(BeanUtils.toBean(city, CityRespVO.class));
    }

    @GetMapping("/all")
    @Operation(summary = "获得所有城市")
    public CommonResult<List<CityRespVO>> getCityList() {
        return success(cityService.getCityList());
    }

    @GetMapping("/page")
    @Operation(summary = "获得城市分页")
    @PreAuthorize("@ss.hasPermission('business:city:query')")
    public CommonResult<PageResult<CityRespVO>> getCityPage(@Valid CityPageReqVO pageReqVO) {
        PageResult<CityDO> pageResult = cityService.getCityPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CityRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出城市 Excel")
    @PreAuthorize("@ss.hasPermission('business:city:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCityExcel(@Valid CityPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CityDO> list = cityService.getCityPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "城市.xls", "数据", CityRespVO.class,
                        BeanUtils.toBean(list, CityRespVO.class));
    }

}